package com.asiainfo.complier.editor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.swing.text.Segment;

import com.asiainfo.complier.editor.JavaKeyWord.Types;

public class KeyWord {

	private static ArrayList<JavaKeyWord> list = new ArrayList<JavaKeyWord>();

	public KeyWord() {
	}

	static {
		InputStream fs = ClassLoader
				.getSystemResourceAsStream("com/asiainfo/complier/conf/Java.cfg");
		BufferedReader reader = new BufferedReader(new InputStreamReader(fs));
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {

				if (!line.startsWith("#")) {
					JavaKeyWord keyWord = new JavaKeyWord();
					keyWord.setName(line.trim());
					keyWord.setType(Types.CLASS);
					list.add(keyWord);

				}
			}
			reader.close();
		} catch (IOException e) {

			e.printStackTrace();
		}

		// KEYWORDS = new String[list.size()];

	}

	/**
	 * 
	 * @param seg
	 * @return
	 */

	public static boolean isKeyWord(Segment seg) {
		boolean isKey = false;
		for (int i = 0; !isKey && i < list.size(); i++)
			if (seg.count == list.get(i).getName().length()) {
				isKey = true;
				for (int j = 0; isKey && j < seg.count; j++)
					if (seg.array[seg.offset + j] != list.get(i).getName()
							.charAt(j))
						isKey = false;

			}
		return isKey;
	}

	// private static String[] KEYWORDS;
}
